﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Comet_sample.Models
{
    public class CometAsyncResult : IAsyncResult
    {
        private bool _isCompleted;
        private HttpContext _httpContext;
        private AsyncCallback _asyncCallback;
        private object _extraData;

        public CometAsyncResult(HttpContext httpContext, AsyncCallback asyncCallback, Object extraData)
        {
            _httpContext = httpContext;
            _asyncCallback = asyncCallback;
            _extraData = extraData;
        }

        public HttpContext HttpContext { get { return _httpContext; } }

        public object AsyncState
        {
            get { return null; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { return null; }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return _isCompleted; }
        }

        public void SendMessage(string message)
        {
            _httpContext.Response.Write(message);
            if (_asyncCallback != null)
            {
                _asyncCallback(this);
            }
            _isCompleted = true;
        }
    }
}